package com.modai.springcloud.entities;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.modai.springcloud.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author ：modai
 * @date ：Created in 2020-12-18 20:41
 * @description：
 */
@RestController
@Slf4j
public class OrderController {

    private String URL = "http://nacos-payment-provider";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/fallback/{id}")
//    @SentinelResource(fallback = "fallback")
    @SentinelResource(fallback = "ff")
    public CommonResult<Payment> fallback(@PathVariable("id")Long id){

        CommonResult<Payment> forObject = restTemplate.getForObject(URL + "/payment/" + id, CommonResult.class);
        if (id>=4){
            throw  new IllegalArgumentException("参数不能大于4，异常参数");
        }else if (forObject.getData().getSerial()==null){
            throw new NullPointerException("该ID没有指定的值，空指针异常。");
        }
        return forObject;
    }


    public CommonResult<Payment> ff(@PathVariable("id")Long id,Throwable e){
        return new CommonResult<>(444,"出错了，"+id+"\t"+e.getMessage());
    }



    //========================

    @Resource
    private OrderService orderService;


    @GetMapping("/consumer/payment/{id}")
    public CommonResult<Payment> getPayment(@PathVariable(value = "id") Long id){
        return orderService.pay(id);

    }
}
